package cn.yunhe.servlet;
import cn.yunhe.bean.Hotel;
import cn.yunhe.bean.HotelArea;
import cn.yunhe.bean.HotelSearchPage;
import cn.yunhe.biz.HotelAreaService;
import cn.yunhe.biz.HotelService;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/GetHotelByContextServlet")
public class GetHotelByContextServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        HotelService hotelService = new HotelService();
        HotelAreaService hotelAreaService = new HotelAreaService();
        //获取查询酒店目的地址
        String  destination =  request.getParameter("destination");
        //根据目的地址查询获取对应的区域类
        HotelArea hotelArea =  hotelAreaService.getHotelareaByName(destination);
        /*  //获取区域id
        int areaid = hotelArea.getHotelAreaId();*/

        //获取酒店区域地址
        String hotelSiteSearch = request.getParameter("hotelSiteSearch");
        //获取酒店名字
        String  searchhotelname =  request.getParameter("searchhotelname");
        //获取酒店价格
        String  radiohotelsale= request.getParameter("radiohotelsale");
        //获取酒店星级
        String hotelstar = request.getParameter("hotelstar");

        //获取价格 口碑星级 默认 排序 关键词
        String hotelSorted = request.getParameter("hotelSorted");
        //获取酒店分页 的当前业数
        int hotelPageNo = 1 ;
        String strhotelPageNo = request.getParameter("hotelPageNo");
        if(strhotelPageNo != null){
            hotelPageNo = Integer.parseInt(strhotelPageNo);
        }
        //建立 搜索酒店条件  排序  实体类的条件
        HotelSearchPage hotelSearchPage = new HotelSearchPage();
        hotelSearchPage.setHotelSort(hotelSorted);

        //建 酒店实体类
        Hotel hotel = new Hotel();
        hotel.setHotelName(searchhotelname);
        if(hotelSiteSearch != null){
            hotel.setSite(hotelSiteSearch.trim());
        }

        if( hotelstar != null && hotelstar != ""){
            hotel.setFavorite(Integer.parseInt(hotelstar));
        }
        if( radiohotelsale != null && radiohotelsale != ""){
            hotel.setPrice(Integer.parseInt(radiohotelsale));
        }

        hotel.setHotelarea(hotelArea);

        hotelSearchPage.setHotel(hotel);

        //根据酒店条件查询对应的列数
        int hotelCount = hotelService.getCountByHotel(hotelSearchPage);
        //根据总列数 取得对应多少列
        hotelCount =(hotelCount %hotelSearchPage .getPageSize() == 0 ? hotelCount / hotelSearchPage.getPageSize() : hotelCount / hotelSearchPage.getPageSize() + 1);
        //判断分页开始页数
        if(hotelCount>0){
            if (hotelPageNo-1 < 0) {
                hotelPageNo = 1;
            } else if (hotelPageNo-1 >= hotelCount) {
                hotelPageNo = hotelCount;
            }
        }
        //确定每次分页开始的页数
        int pageStartNo = (hotelPageNo-1)*hotelSearchPage.getPageSize() ;

        hotelSearchPage.setPageStartNo(pageStartNo);
        //查询酒店搜索条件 排序查询结果
        List<Hotel> listhotel =  hotelService.getHotelByHotel(hotelSearchPage);
        List<HotelSearchPage> listHotelPage = new ArrayList<>() ;
        for (Hotel hotelresult:listhotel) {
            HotelSearchPage  hotelpage=  new HotelSearchPage();
            hotelpage.setHotel(hotelresult);
            hotelpage.setPageStartNo(hotelPageNo);
            hotelpage.setHotelCount(hotelCount);
            listHotelPage.add(hotelpage);
        }

        response.setHeader("Cache-Control","no-cache");
        response.setContentType("text/json ; charset=UTF-8");
        out.print(JSONArray.fromObject(listHotelPage).toString());
        out.close();
    }
}